package com.suos.core.utils;

import cn.hutool.core.util.StrUtil;
import com.suos.core.entity.SysInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 私有专属工具
 *
 * @author : MisShi
 * @date: 2021-10-20 17:03:45
 */
public class PriUtil {

    public static List<SysInfo> sysInfos = new ArrayList<>();

    private final static String DEF_PORT = "80";

    private final static String HTTP_PREFIX = "http://";


    private final static long KB = 1024;
    private final static long MB = KB * 1024;
    private final static long GB = MB * 1024;
    private final static long TB = GB * 1024;

    /**
     * cpu jvm memory 转换
     *
     * @param size 大小
     * @return 转换后值
     */
    public static String jcmSize(long size) {
        if (size >= TB) {
            return String.format("%.2f T", (float) size / TB);
        } else if (size >= GB) {
            return String.format("%.2f G", (float) size / GB);
        } else if (size >= MB) {
            float f = (float) size / MB;
            return String.format(f > 100 ? "%.2f M" : "%.2f MB", f);
        } else if (size >= KB) {
            float f = (float) size / KB;
            return String.format(f > 100 ? "%.2f K" : "%.2f KB", f);
        } else {
            return String.format("%d B", size);
        }
    }

    /**
     * disk 转换
     *
     * @param size 字节大小
     * @return 转换后值
     */
    public static String diskSize(long size) {
        if (size >= TB) {
            return String.format("%.2f TB", (float) size / TB);
        } else if (size >= GB) {
            return String.format("%.2f GB", (float) size / GB);
        } else if (size >= MB) {
            float f = (float) size / MB;
            return String.format(f > 100 ? "%.2f MB" : "%.2f MB", f);
        } else if (size >= KB) {
            float f = (float) size / KB;
            return String.format(f > 100 ? "%.2f KB" : "%.2f KB", f);
        } else {
            return String.format("%d B", size);
        }
    }


    /**
     * 截取ip
     *
     * @param registerUrl 注册地址
     * @return ip
     */
    public static String getHost(String registerUrl) {
        return getMsg(registerUrl, true);
    }


    /**
     * 截取 port
     *
     * @param registerUrl 注册地址
     * @return ip
     */
    public static int getPort(String registerUrl) {
        String msg = getMsg(registerUrl, false);
        return Integer.parseInt(msg);
    }

    /**
     * @param registerUrl 注册的地址
     * @param orHost      是否获取 host (true: 获取host ; false: 获取port)
     * @return 截取需要的信息
     */
    private static String getMsg(String registerUrl, boolean orHost) {
        registerUrl = registerUrl.toLowerCase();
        registerUrl = registerUrl.replaceAll(HTTP_PREFIX + "s", "").replaceAll(HTTP_PREFIX, "");
        if (orHost) {
            return StrUtil.subBefore(registerUrl, ":", true);
        } else {
            return StrUtil.isNotBlank(StrUtil.subAfter(registerUrl, ":", true)) ? StrUtil.subAfter(registerUrl, ":", true) : DEF_PORT;
        }
    }


}
